/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package geowiki;

import processing.core.PApplet;
import geowiki.utils.SkaleInterpolator; // import processing.core.PImage;
import processing.core.PVector;

/**
 * 
 * @author marctiedemann
 */
public class GeoInterpolTestApp extends PApplet {

	SkaleInterpolator skale;

	// set the parameters for warp
	protected int r0 = 100;
	protected int r1 = 180;

	protected float zoom = 4;
	// /

	protected float s1 = 1 / zoom;
	// protected float s1 = 0.1f;

	PVector center = new PVector();

	// grid parameters
	int xMax = 1200;
	int yMax = 800;
	int step = 10;
	// /

	int totalUSteps = xMax / step;
	int totalVSteps = yMax / step;
	PVector[][] orgGrid = new PVector[totalUSteps][totalVSteps];
	PVector[][] newGrid = new PVector[totalUSteps][totalVSteps];

	// PImage world;
	public void setup() {
		size(1200, 600, P3D);

		// world = loadImage("data/worldmap-equirectangular-blank-dark2.png");
		skale = new SkaleInterpolator(zoom, r0, r1, s1);

		// /set original Grid
		for (int u = 0; u < totalUSteps; u++) {
			for (int v = 0; v < totalVSteps; v++) {
				orgGrid[u][v] = new PVector(u * step, v * step, 0);
			}
		}
	}

	public void draw() {
		background(255);

		// magnifyed area
		center.set(mouseX, mouseY, 0);

		// / skale and interpole grid for magnifyed area
		for (int u = 0; u < totalUSteps; u++) {
			for (int v = 0; v < totalVSteps; v++) {
				newGrid[u][v] = skale.interpolVector(center, orgGrid[u][v]);
			}
		}

		// Draw Simple Mesh in background
		noFill();
		stroke(100, 0, 0, 25);
		for (int v = 0; v < totalVSteps - 1; v++) {
			beginShape(QUAD_STRIP);
			for (int u = 0; u < totalUSteps; u++) {

				vertex(orgGrid[u][v].x, orgGrid[u][v].y, 0);
				vertex(orgGrid[u][v + 1].x, orgGrid[u][v + 1].y, 0);

			}
			endShape();
		}

		// Draw "magnifyed" Mesh
		stroke(0);
		for (int v = 0; v < totalVSteps - 1; v++) {
			beginShape(QUAD_STRIP);
			for (int u = 0; u < totalUSteps; u++) {

				vertex(newGrid[u][v].x, newGrid[u][v].y, 0);
				vertex(newGrid[u][v + 1].x, newGrid[u][v + 1].y, 0);

			}
			endShape();
		}

		// show magnified area
		stroke(255, 0, 0);
		ellipse(center.x, center.y, r0 * 2, r0 * 2);
		ellipse(center.x, center.y, r1 * 2, r1 * 2);

	}
}
